package com.mofang.powerdekorhelper.activity.shopactivity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.adapter.ShopActivityAdapter;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewAdapter;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewList;
import com.mofang.powerdekorhelper.model.ShopActive;
import com.mofang.powerdekorhelper.persenter.ShopActivityPersenter;
import com.mofang.powerdekorhelper.utils.LoadPrograss;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.ShopActivityView;
import com.mofang.powerdekorhelper.witget.MySwipeRefreshLayout;
import com.mofang.powerdekorhelper.witget.TitleBar;

import butterknife.BindView;

/**
 * Created by luofei on 2017/7/4 0004.
 */

public class ShopActiveActivity extends MvpActivity<ShopActivityView, ShopActivityPersenter> implements
        ShopActivityView, BaseRecycleViewList.MyRecycleOnScrollListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.shop_activity_title)
    TitleBar mTitleBar;

    @BindView(R.id.shop_activity_recycle)
    BaseRecycleViewList recycleViewList;

    @BindView(R.id.shop_activity_refersh)
    MySwipeRefreshLayout refreshLayout;

    ShopActivityAdapter adapter;

    private LoadPrograss loadPrograss;

    private int page = 1;
    private int pageSize = 5;
    private Integer shopId;


    @Override
    public void initView() {
        inflateLayout(R.layout.activity_shop_active_layout);
        initTitleBarWithback(mTitleBar, R.string.activity_title);
        refreshLayout.setOnRefreshListener(this);
        recycleViewList.setListOnScrollListener(this);

        loadPrograss = new LoadPrograss(this);
    }

    @Override
    public void initData() {

        shopId = SharePerforenceUtils.getInstance(this).getShopId();
        presenter.getBrandActivity(page, pageSize, shopId);
    }

    @Override
    public void setBrandActivity(ShopActive shopActive) {

        if (shopActive.getResult().getData() != null) {
            if (shopActive.getResult().getData().size() > 0) {
                if (page == 1) {
                    adapter = new ShopActivityAdapter(shopActive.getResult().getData(), this, R.layout.shop_active_list_item_layout);

                    adapter.setOnItemClickListner(new BaseRecycleViewAdapter.OnItemClickListner() {
                        @Override
                        public void onItemClickListner(ViewGroup parent, View v, int position) {

                            ShopActive.Data data = (ShopActive.Data) v.getTag();
                            startActivity(new Intent(ShopActiveActivity.this, ShopActiveDetailActivity.class).putExtra("ActivityId", data.getId()));
                        }
                    });

                    recycleViewList.setAdapter(adapter);
                } else {
                    adapter.addData(shopActive.getResult().getData());
                }
            } else {
                toast("暂无更多活动");
            }
        } else {
            toast("暂无活动");
        }

    }

    @Override
    public ShopActivityPersenter initPresenter() {
        return new ShopActivityPersenter();
    }

    @Override
    public void onRefresh() {
        page = 1;
        pageSize = 5;
        presenter.getBrandActivity(page, pageSize, shopId);
    }

    @Override
    public void loadMore() {
        page++;
        presenter.getBrandActivity(page, pageSize, shopId);
    }

    @Override
    public void showProgress() {
        loadPrograss.show();
    }

    @Override
    public void hideProgress() {
        loadPrograss.hide();
        refreshLayout.hide();
    }

    @Override
    public void toast(CharSequence s) {

        T.showShort(this, s);
    }

    @Override
    public void toast(int id) {

    }

    @Override
    public void showNullLayout() {

    }

    @Override
    public void hideNullLayout() {

    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {
        hideProgress();
    }

    @Override
    public void hideErrorLayout() {

    }

    @Override
    public void onError(String errorInfo) {

    }
}
